package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.PropertyScrap;
import com.ruoyi.system.domain.vo.WarehousingInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PropertyBasicsInfoMapper;
import com.ruoyi.system.domain.PropertyBasicsInfo;
import com.ruoyi.system.service.IPropertyBasicsInfoService;

/**
 * 资产信息基础管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-08
 */
@Service
public class PropertyBasicsInfoServiceImpl implements IPropertyBasicsInfoService 
{
    @Autowired
    private PropertyBasicsInfoMapper propertyBasicsInfoMapper;
    @Autowired
    private PropertyScrapServiceImpl propertyScrapService;

    /**
     * 查询资产信息基础管理
     * 
     * @param id 资产信息基础管理主键
     * @return 资产信息基础管理
     */
    @Override
    public PropertyBasicsInfo selectPropertyBasicsInfoById(Long id)
    {
        return propertyBasicsInfoMapper.selectPropertyBasicsInfoById(id);
    }

    /**
     * 查询资产信息基础管理列表
     * 
     * @param propertyBasicsInfo 资产信息基础管理
     * @return 资产信息基础管理
     */
    @Override
    public List<PropertyBasicsInfo> selectPropertyBasicsInfoList(PropertyBasicsInfo propertyBasicsInfo)
    {
        return propertyBasicsInfoMapper.selectPropertyBasicsInfoList(propertyBasicsInfo);
    }

    /**
     * 新增资产信息基础管理
     * 
     * @param propertyBasicsInfo 资产信息基础管理
     * @return 结果
     */
    @Override
    public int insertPropertyBasicsInfo(PropertyBasicsInfo propertyBasicsInfo)
    {
        if (propertyBasicsInfoMapper.countPropertyBasicsInfoByCode(propertyBasicsInfo.getCode()) > 0 ){
            throw new ServiceException("资产编号重复", 500);
        }
        propertyBasicsInfo.setCreateTime(DateUtils.getNowDate());
        propertyBasicsInfo.setIsStorage("N");
        propertyBasicsInfo.setIsLoan("N");
        return propertyBasicsInfoMapper.insertPropertyBasicsInfo(propertyBasicsInfo);
    }

    /**
     * 修改资产信息基础管理
     * 
     * @param propertyBasicsInfo 资产信息基础管理
     * @return 结果
     */
    @Override
    public int updatePropertyBasicsInfo(PropertyBasicsInfo propertyBasicsInfo)
    {
        propertyBasicsInfo.setUpdateTime(DateUtils.getNowDate());
        return propertyBasicsInfoMapper.updatePropertyBasicsInfo(propertyBasicsInfo);
    }

    /**
     * 批量删除资产信息基础管理
     * 
     * @param ids 需要删除的资产信息基础管理主键
     * @return 结果
     */
    @Override
    public Boolean deletePropertyBasicsInfoByIds(Long[] ids)
    {
        for (Long id : ids) {
            PropertyBasicsInfo propertyBasicsInfo = propertyBasicsInfoMapper.selectPropertyBasicsInfoById(id);
            propertyBasicsInfo.setDelFlag("2");
            propertyBasicsInfoMapper.updatePropertyBasicsInfo(propertyBasicsInfo);
            PropertyScrap propertyScrap = new PropertyScrap();
            propertyScrap.setCode(propertyBasicsInfo.getCode());
            propertyScrap.setBasicsId(propertyBasicsInfo.getId());
            propertyScrap.setScrapTime(new Date());
            propertyScrapService.insertPropertyScrap(propertyScrap);
        }
        return true;
    }

    /**
     * 删除资产信息基础管理信息
     * 
     * @param id 资产信息基础管理主键
     * @return 结果
     */
    @Override
    public int deletePropertyBasicsInfoById(Long id)
    {
        return propertyBasicsInfoMapper.deletePropertyBasicsInfoById(id);
    }

    /**
     * 查询资产信息基础管理
     *
     * @param warehousingInfo 资产信息基础管理主键
     * @return 资产信息基础管理
     */
    @Override
    public  List<WarehousingInfo>  warehousingInfoList(WarehousingInfo warehousingInfo)
    {
        return propertyBasicsInfoMapper.warehousingInfoList(warehousingInfo);
    }
}
